<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../static/lib/vue.js"></script>
</head>

<body>
    <div id="app">
        <parent-component></parent-component>
    </div>

    <template id="parent-component">
        <div>
            {{msg}}
            <child-component1></child-component1>
            <child-component2></child-component2>
            <button v-on:click="showChildComponentData">显示子组件的数据-children</button>
            <button v-on:click="showParentComponentData">显示父组件的数据-parent</button>
        </div>
    </template>

    <template id="child-component1">
        <h2>{{msg}}</h2>
    </template>

    <template id="child-component2">
        <h2>{{msg}}</h2>
    </template>


    <script>
        Vue.component('parent-component', {
            template: '#parent-component',
            props: ['msg'],
            components: {
                'child-component1': {
                    template: '#child-component1',
                    data: function () {
                        return {
                            msg: 'child component 111111'
                        }
                    }
                },
                'child-component2': {
                    template: '#child-component2',
                    data: function () {
                        return {
                            msg: 'child component 222222'
                        }
                    }
                }
            },
            methods: {
                showChildComponentData: function () {
                    for (var i = 0; i < this.$children.length; i++) {
                        alert(this.$children[i].msg)
                    }
                },
                showParentComponentData: function () {

                    alert(this.$parent.msg);
                }
            }
        })

        new Vue({
            el: '#app',
            data: {
                msg: 'my is parent'
            }
        })
    </script>
</body>

</html>